package com.example.job.adapter

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import com.example.job.databinding.ViewholderJobBinding
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.job.Activity.JobDetailActivity
import com.example.job.Model.JobModel

class JobAdapter(private val items:List<JobModel>): RecyclerView.Adapter<JobAdapter.ViewHolder>() {
    private lateinit var context:Context

    inner class ViewHolder(val binding: ViewholderJobBinding) :
        RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        context = parent.context
        val binding =
            ViewholderJobBinding.inflate(LayoutInflater.from(context),parent,false)
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val items = items[position]
        holder.binding.title.text = items.title
        holder.binding.company.text = items.company
        holder.binding.type.text = items.type
        holder.binding.salary.text = items.minsalary + " - " + items.maxsalary
        holder.binding.experience.text = items.experience
        holder.binding.edu.text = items.edu
        holder.binding.city.text = items.city
        //图片加载
        Glide.with(holder.itemView.context).load(items.img).into(holder.binding.logo)

        holder.itemView.setOnClickListener{
            val intent = Intent(context, JobDetailActivity::class.java)
            intent.putExtra("object",items)
            holder.itemView.context.startActivity(intent)
        }
    }

    override fun getItemCount(): Int = items.size
}